package com.haizhi.mespush.schedule;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haizhi.mespush.bean.LFPerson;
import com.haizhi.mespush.bean.Person;
import com.haizhi.mespush.job.HostMesPushJob;
import com.haizhi.mespush.job.MesPushJob;
import com.haizhi.mespush.service.LFPersonService;
import com.haizhi.mespush.service.WorkService;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class MyScheduler {

    @Autowired
    WorkService workService;

    private static final int timeout = 10000;

    //下一个值周 no
    public static int index = 1;

    //本周值周下表 no
    public static int hostIndex;


    private static final String dingUrl = "https://oapi.dingtalk.com/robot/send?access_token=4194b5903ef5806677a2eeb2f5a94332b7192c2bd30ba3684b762323eaeae701";

    @Scheduled(cron = "0 15 10 ? * MON")
    @Transactional(rollbackFor = Exception.class)
    public void Megprocess() {
        try {
            List<Person> personList = workService.list();

            if (index == personList.size()) {
                index = 1;
            }
            //通知具体人的手机号码列表   ->初始化
            List<String> mobileList = new ArrayList<String>();
            boolean isAtAll = false;
            //本应该值周的
            Person workPerson = workService.getOne(new QueryWrapper<Person>().eq("no",index));
            mobileList.add(workPerson.getMobile());
            String content = "大家好!本周值周的是";
            //组装请求内容
            String reqStr = buildReqStr(content, isAtAll, mobileList);
            //推送消息（http请求）
            String body = HttpRequest.post(dingUrl).body(reqStr).timeout(timeout).execute().body();
            hostIndex = workPerson.getNo();
            //清除推送列表   ->结束
            mobileList.clear();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("error" + e.getMessage());
        } finally {
            index++;
        }
    }


    @Scheduled(cron = "0 0 15 ? * WED")
    public void HostMegprocess() {
        try {
            Person workPerson = workService.getOne(new QueryWrapper<Person>().eq("no",hostIndex));
            boolean isAtAll = false;
            //通知具体人的手机号码列表
            List<String> mobileList = new ArrayList<String>();
            mobileList.add(workPerson.getMobile());
            //钉钉机器人消息内容
            String content = "大家好!今天主持周会的是";
            //组装请求内容
            String reqStr = buildReqStr(content, isAtAll, mobileList);
            //推送消息（http请求）
            String body = HttpRequest.post(dingUrl).body(reqStr).timeout(timeout).execute().body();
            mobileList.clear();

        }catch (Exception e){
            e.printStackTrace();
            System.out.printf("error"+e.getMessage());
        }
    }

    private static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
        //消息内容
        Map<String, String> contentMap = new HashMap<String, String>();
        contentMap.put("content", content);

        //通知人
        Map<String, Object> atMap = new HashMap();
        //1.是否通知所有人
        atMap.put("isAtAll", isAtAll);
        //2.通知具体人的手机号码列表
        atMap.put("atMobiles", mobileList);

        Map<String, Object> reqMap = new HashMap();
        reqMap.put("msgtype", "text");
        reqMap.put("text", contentMap);
        reqMap.put("at", atMap);

        return JSONUtil.toJsonStr(reqMap);
    }

}
